﻿using System;
using System.Linq.Expressions;
using NHibernate.Expression.Extensions.Helpers;
using NHibernate.Criterion;

namespace NHibernate.Expression.Extensions {
    public static class OrderEx {
        public static Order Create<T>(Expression<Func<T, object>> property, bool ascending) {
            return new Order(property.MemberName(), ascending);
        }

        public static Order Asc<T>(Expression<Func<T, object>> property) {
            return Order.Asc(property.MemberName());
        }

        public static Order Desc<T>(Expression<Func<T, object>> property) {
            return Order.Desc(property.MemberName());
        }

        private static string Prefix(string p, string m) {
            return string.Format("{0}.{1}", p, m);
        }

        private static string Prefix<T>(string p, Expression<Func<T, object>> m) {
            return Prefix(p, m.MemberName());
        }


        public static Order Create<T>(string prefix, Expression<Func<T, object>> property, bool ascending) {
            return new Order(Prefix(prefix, property), ascending);
        }

        public static Order Asc<T>(string prefix, Expression<Func<T, object>> property) {
            return Order.Asc(Prefix(prefix, property));
        }

        public static Order Desc<T>(string prefix, Expression<Func<T, object>> property) {
            return Order.Desc(Prefix(prefix, property));
        }
    }
}